package com.zlp.service.impl;

import com.zlp.custom.sys.ResourceMapper;
import com.zlp.dao.SysUserMapper;
import com.zlp.entity.SysResource;
import com.zlp.entity.SysUser;
import com.zlp.service.SysUserService;
import com.zlp.utils.JwtTokenUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author ：ZouLiPing
 * @date ： 2019/10/8 17:24
 */
@Service
@Slf4j
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private UserDetailsService userDetailsService;

    @Autowired
    private JwtTokenUtil jwtTokenUtil;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private ResourceMapper resourceMapper;

    @Override
    public SysUser getUserByUsername(String userCode) {
        return sysUserMapper.getUserByUsername(userCode);
    }

    @Override
    public List<SysResource> getPermissionList(Integer userId) {
        List<SysResource> permissionList = resourceMapper.getPermissionList(userId);
        return permissionList;
    }

    @Override
    public String login(String username, String password) {

        String token = "";
        try {
            UserDetails userDetails = userDetailsService.loadUserByUsername(username);
            String encodePassword = passwordEncoder.encode("123456");
            log.info("encodePassword=====>:{}",encodePassword);
            if (!passwordEncoder.matches(password, userDetails.getPassword())) {
                throw new BadCredentialsException("密码不正确");
            }
            log.info("第四步======================》验证用户信息");
            UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());
            SecurityContextHolder.getContext().setAuthentication(authentication);
            token = jwtTokenUtil.generateToken(userDetails);
        } catch (AuthenticationException e) {
            log.warn("登录异常:{}", e.getMessage());
        }
        return token;
    }

    @Override
    public SysUser register(SysUser sysUserVO) {
        sysUserVO.setCreateTime(new Date());
        sysUserVO.setStatus((byte) 1);
        sysUserVO.setUserName(UUID.randomUUID().toString());
        sysUserVO.setCreateBy("压侧001");
        //将密码进行加密操作
        String encodePassword = passwordEncoder.encode(sysUserVO.getPassword());
        sysUserVO.setPassword(encodePassword);
        sysUserMapper.insert(sysUserVO);
        return sysUserVO;
    }

    @Override
    public SysUser getUserInfo(String userCode) {
        SysUser sysUser = sysUserMapper.getUserByUsername(userCode);
        return sysUser;
    }
}
